
import sys
from xml.dom.minidom import Document

def atomNode(doc, field_name, value_text):
    result_node = doc.createElement(field_name)
    text_node = doc.createTextNode(value_text)
    result_node.appendChild(text_node)
    return result_node

fi = sys.argv[1]
fo = sys.argv[2]
print fi

# create a document object and append a records node
doc = Document()
profiles = doc.createElement('profiles')
doc.appendChild(profiles)

line_to_skip = 1
for line in open(fi):
    print line
    # skip initial lines
    if line_to_skip > 0:
        line_to_skip -= 1
        continue

    # create a user data node
    user = doc.createElement('profile')

    segs = line.strip().split('\t')
    name = segs[0]
    user.appendChild(atomNode(doc, 'name', name))
    for i in range(1, len(segs)):
        field_name = ''
        if i % 2 == 1:
            field_name = 'job'
        else:
            field_name = 'step'
        item = segs[i]
        if '\"' in item:
            item = segs[i].split('\"')[1]
        item = item.replace(', ', ',')
        user.appendChild(atomNode(doc, field_name, item))

    profiles.appendChild(user)

print doc.toprettyxml()
doc.writexml(open(fo, 'w'), indent = '  ', addindent = '  ', newl='\n')

